#include <caconsumer/CAConsumerE.h>

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



	// CONSTRUCTORES***********************************************

CAConsumer::EDROOMTopContext::EDROOMTopContext (CAConsumer &act ):

	EDROOMcomponent(act)
	,Msg(EDROOMcomponent.Msg)
	,MsgBack(EDROOMcomponent.MsgBack)
	,pIn(EDROOMcomponent.pIn)
	,Timer(EDROOMcomponent.Timer)
 {

}

CAConsumer::EDROOMTopContext::EDROOMTopContext ( EDROOMTopContext &contex ):

	EDROOMcomponent(contex.EDROOMcomponent),
	Msg(contex.Msg),
	MsgBack(contex.MsgBack)
	,pIn(contex.pIn)
	,Timer(contex.Timer)
 {

}

	// EDROOMSearchContextTrans********************************************

bool CAConsumer::EDROOMTopContext::EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans){

	bool edroomValidMsg=false; 

	 switch(Msg->signal){

		 case ( EDROOMSignalDestroy ): 

			 edroomValidMsg=true;
			 edroomCurrentTrans.distanceToContext = 0 ;
			 edroomCurrentTrans.localId = -1 ;
			 break;

	}

	return(edroomValidMsg);

}

	// Funciones Definidas por el usuario  ****************************

void	CAConsumer::EDROOMTopContext::FShowData()

{
 
	TEDROOMByte & varTEDROOMByte = *(TEDROOMByte *)(Msg->data);
   print_string("Data=");
	print_char(varTEDROOMByte);
	print_string("\n");
 
 
}



void	CAConsumer::EDROOMTopContext::FConReady()

{
 
		
	
	pIn.send(SConReady, EDROOMprioNormal);
 
 
 
 
}



void	CAConsumer::EDROOMTopContext::FShowTime()

{
 
 Pr_Time aux;
 aux.GetTime();
 print_string("Current Time=");
 print_uint32((uint32_t)aux.Secns());
 print_string("\n");
 
}
		



void	CAConsumer::EDROOMTopContext::FProgDelay()

{
 
	Pr_Time	interval(1.0); 
	Timer.InformIn( interval, EDROOMprioNormal );
 
 
 
 
}



	//*****Pools



// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



	// CONSTRUCTOR*************************************************

CAConsumer::EDROOMTopState::EDROOMTopState (CAConsumer &act ):
	  EDROOMTopContext( act  )
{

}

	// EDROOMBehaviour**********************************************

void CAConsumer::EDROOMTopState::EDROOMBehaviour(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans = EDROOMIllegada();

	do{

		switch(edroomCurrentTrans.localId){

			case(Init):
				edroomNextState = Ready;
				break;
			case(ShowData):
				FShowData();
				edroomNextState = HandleData;
				break;
			case(EndHandling):
				FShowTime();
				edroomNextState = Ready;
				break;
		}

		switch(edroomNextState){

			case(I):
				edroomCurrentTrans=EDROOMIllegada();
				break;

			case(Ready):
				FConReady();
				edroomCurrentTrans=EDROOMReadyllegada();
				break;

			case(HandleData):
				FProgDelay();
				edroomCurrentTrans=EDROOMHandleDatallegada();
				break;

		}

		edroomCurrentState=edroomNextState;

	}while(Msg->signal != EDROOMSignalDestroy);

}



	// Inicio**********************************************

void CAConsumer::EDROOMTopState::EDROOMInicio(){

edroomCurrentState=I;

}



//	 ***********************************************************************

//	 llegada SubEstado I

//	 ***********************************************************************



TEDROOMTransId CAConsumer::EDROOMTopState::EDROOMIllegada(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans.localId = Init;
	edroomCurrentTrans.distanceToContext = 0;
	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja Ready

	// ***********************************************************************



TEDROOMTransId CAConsumer::EDROOMTopState::EDROOMReadyllegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		switch(Msg->signal){

			case (SData): 

				 if ( *Msg->GetPInterface() == pIn) {

					edroomCurrentTrans.localId= ShowData;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

		};

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja HandleData

	// ***********************************************************************



TEDROOMTransId CAConsumer::EDROOMTopState::EDROOMHandleDatallegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		switch(Msg->signal){

			case (EDROOMSignalTimeout): 

				 if ( *Msg->GetPInterface() == Timer) {

					edroomCurrentTrans.localId= EndHandling;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

		};

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	return(edroomCurrentTrans);

}



